import 'package:flutter/material.dart';

class CircularCountdown extends StatefulWidget {
  final int seconds;
  final VoidCallback onFinish;
  final Color color;
  final double size;
  const CircularCountdown(
      {super.key,
      required this.seconds,
      required this.onFinish,
      this.color = Colors.blue,
      this.size = 120});
  @override
  State<CircularCountdown> createState() => _CircularCountdownState();
}

class _CircularCountdownState extends State<CircularCountdown>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late int _currentSeconds;

  @override
  void initState() {
    super.initState();
    _currentSeconds = widget.seconds;
    _controller = AnimationController(
      vsync: this,
      duration: Duration(seconds: widget.seconds),
    )
      ..addListener(() {
        setState(() {
          _currentSeconds =
              widget.seconds - (_controller.value * widget.seconds).floor();
        });
      })
      ..forward().whenComplete(widget.onFinish);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      alignment: Alignment.center,
      children: [
        SizedBox(
          width: widget.size,
          height: widget.size,
          child: CircularProgressIndicator(
            value: 1 - _controller.value,
            strokeWidth: 8,
            color: widget.color,
            backgroundColor: widget.color.withOpacity(0.2),
          ),
        ),
        Text('$_currentSeconds',
            style: const TextStyle(fontSize: 32, fontWeight: FontWeight.bold)),
      ],
    );
  }
}
